package org.msfj.authority.client.util;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import org.msfj.common.annotation.Constants;

/**
 * @Description:
 * @Author: like
 * @Date: 2017-12-20 15:19
 * @Version: 1.0
 * @Created in idea by autoCode
 */
public class ClientSecureUtil {
    public final static byte[] key= new byte[]{100,101,121,97,116,111,110,103,100,101,121,97,116,111,110,103};
    public static String encrypt(String clientName) {
        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key);
        return aes.encryptHex(Constants.CLIENT_SECRET_CHARACTER + clientName);
    }

    public static String  decrypt(String encryptHex){
        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key);
        String decryptStr = aes.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);
        return decryptStr;
    }

    public static boolean isValidate(String token){
        String[] strings = token.split(";");
        String decryptStr = decrypt(strings[1]);
        return StrUtil.isNotEmpty(decryptStr) && decryptStr.equals(Constants.CLIENT_SECRET_CHARACTER + strings[0]);
    }

    public static void main(String[] args) {
        System.out.println(encrypt("toilet-service"));
    }
}
